package com.ln.cmsln.controller.bai;

import com.ln.cmsln.pojo.entity.bai.Permission;
import com.ln.cmsln.pojo.entity.bai.Role;
import com.ln.cmsln.pojo.entity.bai.User;
import com.ln.cmsln.service.bai.RoleService;
import com.ln.cmsln.service.bai.UserService;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * @author baiyuxiang
 * @description 用户信息
 * @date 2022/3/31 19:52
 * @Version 1.0
 **/
@RestController
@RequestMapping("/uc")
@CrossOrigin
public class UserController{
    @Resource
    private UserService userService;

    @Resource
    private RoleService roleService;

    //用户登录
    @RequestMapping("/dologin")
    @ResponseBody
    public String dologin(User user, HttpSession session){
        //根据用户密码查询到对象
        User usess = userService.getUsess(user);

        session.setAttribute("loginUser", usess);

        //创建一个map集合
        Map<String,Object> map=new HashMap<>();
        if(usess!=null){
            //设置一个权限为空
            Permission root=null;
            //登录用户的id获取所拥有的权限
            List<Permission> listper=userService.findurp(usess.getId());
            //第一个循环：将其循环存入到map中去
            for (Permission permission : listper) {
                map.put(permission.getId(), permission);
            }
            //第二个循环：
            for (Permission permission : listper) {
                //把所有节点看成子节点
                Permission  child=permission;
                if(child.getPid().equals("0")){
                    //父节点
                    root=child;
                }else{
                    //子节点
                    Permission parent = (Permission) map.get(child.getPid());
                    parent.getChildren().add(child);
                }
            }
            session.setAttribute("root", root);
            return "1";
        }else{
            return "-1";
        }
    }

    //用户信息全查
    @RequestMapping("/findall")
    public Object findall(){
        List<User> list = userService.findAllinfo();
        HashMap<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("count", 0);
        map.put("data", list);
        return map;
    }

    //用户单个删除
    @RequestMapping("/delUser")
    @ResponseBody
    public String delUser(User user){

        try {
            userService.delUser(user);
            return "ok";
        } catch (Exception e) {
            e.printStackTrace();
            return "no";
        }
    }

    //用户批量删除
    @RequestMapping("/deluserss")
    @ResponseBody
    public String delss(String  id){
        try {
            String[] split = id.split(",");
            List<String> list = Arrays.asList(split);
            userService.delss(list);
            return "ok";
        } catch (Exception e) {
            e.printStackTrace();
            return "no";
        }

    }

    //用户添加
    @RequestMapping("/addUser")
    @ResponseBody
    public String addUser(User user){

        try {
            userService.addUser(user);
            return "ok";
        } catch (Exception e) {
            e.printStackTrace();
            return "no";
        }

    }

    //用户去修改界面
    @RequestMapping("/up1")
    public String toUpdate(User user,Model model){
        try {
            //根据id查询xuesheng信息
            User stu = userService.toUpdate(user);
            model.addAttribute("user",stu);
            return "user/edit";
        } catch (Exception e) {
            e.printStackTrace();
            return "no";
        }

    }

    //用户修改
    @RequestMapping("/up")
    @ResponseBody
    public String updateInfo(User user){
        try {
            userService.updateInfo(user);
            return "ok";
        } catch (Exception e) {
            e.printStackTrace();
            return "no";
        }

    }

    //跳转到用户附角色页面
    @RequestMapping("/asignUser")
    public String asignUser(User user,Model model){

        //根据id查询xuesheng信息
        //全查中间表
        //List<Role> unlistrole = userService.asignUserOne(user);

        //全查角色表
        List<Role> unrole = roleService.findAllinfo();

        //查询角色的list集合
        //List<Role> dolistrole = userService.asignUserTwo(user);
        List<String> list = userService.queryrolesing(user);
        List<Role> dorole = userService.asignUserTwo(user);

        //未分配角色列表
        List<Role>  unlistrole = new ArrayList<>();
        //已分配角色列表
        List<Role>  dolistrole = new ArrayList<>();

        for (Role role : unrole) {
            //全查的中的id是否包含list中的id
            if(list.contains(role.getId())){
                dolistrole.add(role);
            }else{
                unlistrole.add(role);
            }
        }

        model.addAttribute("unrole",unrole);
        model.addAttribute("unlistrole",unlistrole);
        model.addAttribute("dolistrole",dolistrole);


        return "user/asign";

    }

    //用户批量添加角色
    @RequestMapping("/unrole")
    @ResponseBody
    public String unrole(String roleun,String userid){

        try {
            String[] split = roleun.split(",");
            List<String> list = Arrays.asList(split);
            userService.unrole(userid,list);
            return "ok";
        } catch (Exception e) {
            e.printStackTrace();
            return "no";
        }

    }

    //用户批量删除角色
    @RequestMapping("/dorole")
    @ResponseBody
    public String dorole(String doroel,String userid){
        System.out.println(doroel);
        System.out.println(userid);
        try {
            String[] split = doroel.split(",");
            List<String> list = Arrays.asList(split);

            userService.dorole(userid,list);
            return "ok";
        } catch (Exception e) {
            e.printStackTrace();
            return "no";
        }
    }
}
